Lab 17

  1. Write a function called thirdDigit. The function has an integer parameter and returns the third digit in its parameter. If the parameter is less than 100 the function returns 0 because there is no third digit.
  2. For example, a program that uses the function follows.
    int main() { cout << thirdDigit(347) << " " << thirdDigit(2048) << " " << thirdDigit(560125) << endl; return 0; }

    It should print:
    7 4 0

  3. Write a function called useRecursion that returns the sum of the first two digits in a positive number. If there is only one digit, that digit is returned. For example, a program that uses the function useRecursion follows.
  4. int main() { cout << useRecursion(567982) << endl; // prints 11 cout << useRecursion(107982) << endl; // prints 1 cout << useRecursion(7) << endl; // prints 7 return 0; }

  5. Write a function called unlucky that returns an answer of true if the first two digits of a positive integer parameter add to 13. Otherwise it returns false. (It returns false if the parameter has fewer than 2 digits.)
  6. For example, a program that uses the function unlucky follows.
    int main() { int x = 6789; if (unlucky(x)) cout << x << " is Unlucky!\n"; // prints 6789 is Unlucky! x = 6889; if (unlucky(x)) cout << x << " is Unlucky!\n"; // prints x = 6; if (unlucky(x)) cout << x << " is Unlucky!\n"; // prints x = 49; if (unlucky(x)) cout << x << " is Unlucky!\n"; // prints 49 is Unlucky! return 0; }